<?php
class GraphComponent extends Object {

	var $controller = null;
	var $year;
	var $laundry;
	var $type;
	var $subtype;
	var $benchmark;
	
	function initialize(&$controller) {
		$this->controller = $controller;
		
		$this->Measurement = Classregistry::init('Measurement');
		$this->CacheUsage = Classregistry::init('CacheUsage');
	}
	
	function getData() {
		$data = array();
		if($this->controller->data) {
			$this->year = $this->controller->data['Graph']['date']['year'];
			$this->laundry = $this->controller->params['laundry'];
			$this->type = $this->controller->data['Graph']['type'];
			$this->subtype = $this->controller->data['Graph']['subtype'];
			$this->benchmark = (!empty($this->controller->data['Graph']['benchmark']) && $this->controller->data['Graph']['benchmark'] == '1') ? true : false;
						
			$data['subtitle'] = 'Laundry: ' . $this->Measurement->Laundry->field('Laundry.name', array('Laundry.id' => $this->laundry));
			
			$data['series'] = array();
			if(in_array($this->type, array('productiontypes', 'usagetypes'))) {
				$data['series'] = $this->{'graph' . ucfirst($this->type)}();
			}
			
			$data['xAxis']['title']['text'] = 'Measurement';
			for($i = 0; $i < 12; $i++) {
				$data['xAxis']['categories'][$i] = date('F Y', mktime(0, 0, 0, $i + 1, 1, $this->year));
				if(empty($data['series'][0]['data'][$i])) {
					$data['series'][0]['data'][$i] = 0;
				}
				if($this->benchmark) {
					if(empty($data['series'][1]['data'][$i])) {
						$data['series'][1]['data'][$i] = 0;
					}
				}
			}
		}
		return $data;
	}
	
	function graphProductiontypes() {
		return $this->__basicGraph('Production', $this->subtype);
	}	
	
	function graphUsagetypes() {
		if(in_array($this->subtype, array('cost', 'emission'))) {
			return $this->__costGraph('Usage', $this->subtype);
		}
		return $this->__basicGraph('Usage', $this->subtype);
	}	
	
	function __basicGraph($type, $subtype) {
		$data[0] = array('name' => 'Own laundry', 'data' => array());
		$measurements = $this->Measurement->find('all', array(
			'conditions' => array(
				'Measurement.laundry_id' => $this->laundry,
				'Measurement.year' => $this->year,
			)
		));
		
		foreach($measurements AS $measurement) {
			foreach($measurement[ucfirst($type)] AS $array) {
				if($array[strtolower($type) . 'type_id'] == $this->controller->data['Graph']['id']) {
					$data[0]['data'][$measurement['Measurement']['month']-1] = intval($array['amount']);
				}
			}
		}	
		
		if($this->benchmark) {
			$data[1] = array('name' => 'Benchmark', 'data' => array());
			$avg = $this->Measurement->findAvgByYear($this->year, $type, $this->controller->data['Graph']['id']);
			for($i = 0; $i < 12; $i++) {
				if(!empty($avg[$i+1])) {
					$data[1]['data'][intval($i)] = $avg[$i+1];
				}
			}
		}
		return $data;
	}
	
	function __costGraph($type, $subtype) {
		$data[0] = array('name' => 'Own laundry', 'data' => array());
		foreach($this->CacheUsage->findTotalByLaundryYear($this->laundry, $this->year) AS $month => $cacheusage) {
			$data[0]['data'][$month-1] = intval($cacheusage[$subtype]);
		}
		
		if($this->benchmark) {
			$data[1] = array('name' => 'Benchmark', 'data' => array());
			foreach($this->CacheUsage->findAvgByYear($this->year) AS $month => $cacheusage) {
				$data[1]['data'][$month-1] = intval($cacheusage[$subtype]);
			}
		}
		return $data;
	}
}